<html>
	<head>
		<script>

			function onLoad() {
			}



			function testSubmission() {
				var element = document.getElementById("nameElement");
				name = element.value;
				
				element = document.getElementById("keyElement");
				key = element.value;
				
				var url = "version/" + name + "/" + key;
				submit("GET",url,null,versionComplete);
			}
			
			var version;
			
			function versionComplete(result) {
				alert("Version request complete: " + result);
				var versionInfo = eval('(' + result + ')');
				version = versionInfo.version;
				
				var bodyElement = document.getElementById("bodyElement");
				var data = bodyElement.value;
				var url = "file/" + name + "/" + key + "/" + version;
				submit("PUT",url,data,dataComplete);
			}
			
			function dataComplete(result) {
				alert("Post complete");
				var url = "file/" + name + "/" + key + "/" + version;
				submit("GET",url,null,downloadComplete);
			}
			
			function downloadComplete(result) {
				alert(result);
				
				var url = "file/" + name + "/" + key;
				submit("GET",url,null,download2Complete);
			}
			
			function download2Complete(result) {
				alert(result);
			}

			function submit(method,url,body,onDownload) {

				var xmlhttp = new XMLHttpRequest();

				xmlhttp.onreadystatechange=function() {
					if (xmlhttp.readyState==4 && xmlhttp.status==200) {
						onDownload(xmlhttp.responseText);

					}
					else if(xmlhttp.readyState==4) {
						alert("Error in request, Status: " + xmlhttp.status);
					}
				}

				xmlhttp.open(method,url,true);
				xmlhttp.send(body);
			}

			function clearResult() {
				$("#resultDiv").html("");
			}

		</script>
	</head>
	<body onload="onLoad()">
		<h1>Upload Test</h1>
		name: <textarea id="nameElement" cols="31"></textarea><br>
		key: <textarea id="keyElement" cols="31"></textarea><br>
		file: <textarea id="bodyElement" cols="70" rows="20"></textarea><br>
		<input type="button" value="Submit" onclick="testSubmission()"/>
	</body>
</html>
